<template>
  <div class="table-wrap" v-loading="loading">
    <div class="search_header">
      <div class="search_header_left">
        <el-button
          icon="el-icon-plus"
          @click="addProject"
          v-has="'fileManagement:add'"
          >新增</el-button
        >
        <!-- <el-button
          icon="el-icon-download"
          @click="downloadFile"
          v-has="'fileManagement:export'"
          >下载数据</el-button
        > -->
      </div>

      <div class="search_header_right">
        <el-input
          placeholder="请输入文件名称"
          v-model="parameter.title"
          @keydown.enter.native="search"
          clearable
          @clear="search"
        >
          <span
            @click="search"
            slot="suffix"
            style="display: inline-block"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
        <!-- <el-select
          placeholder="类型"
          v-model="parameter.projectType"
          clearable
          @change="search"
        >
          <el-option
            v-for="item in civilTypeOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select> -->
      </div>
    </div>
    <el-table
      :data="projectList"
      :header-cell-style="{
        background: '#F2F6FB',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
    >
      <el-table-column
        label="序号"
        align="center"
        show-overflow-tooltip
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="文件名称"
        prop="title"
        show-overflow-tooltip
      />
      <el-table-column
        label="索引号"
        prop="indexNumber"
        show-overflow-tooltip
      />
      <el-table-column
        label="生效时间"
        prop="effectTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="发布机构"
        prop="publishUnit"
        show-overflow-tooltip
      />
      <el-table-column
        label="成文日期"
        prop="wordTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="发布日期"
        prop="publishTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="文号"
        prop="number"
        show-overflow-tooltip
      />
      <el-table-column
        label="公文种类"
        prop="fileType"
        show-overflow-tooltip
      />
      <el-table-column
        label="有效性"
        prop="validity"
        show-overflow-tooltip
      />
      <el-table-column
        label="责任单位"
        prop="publishUnit"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
        width="240"
      >
        <template slot-scope="scope">
          <el-button
            @click="viewProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >查看</el-button
          >
          <el-button
            @click="editProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            v-has="'fileManagement:edit'"
            >编辑</el-button
          >
          <el-button
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            v-has="'fileManagement:delete'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
        :current-page="parameter.pageIndex"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { downloadProject } from '@/api/FixedInvestmentTarget'
import {
  queryFileInfo,
  deleteFileInfo
} from '@/api/dataManagement'
import Icon from '@/components/icon'
import { civilTypeOptions } from '@/utils/project/data'
import listKeepAliveMixin from '@/mixins/listKeepAliveMixin'

export default {
  name: 'fileManagementList',
  mixins: [listKeepAliveMixin],
  components: {
    Icon
  },
  data() {
    return {
      projectList: [],
      total: '',
      parameter: {
        title: '',
        pageIndex: 1,
        pageSize: 10
      },
      projectTypeOptions: [],
      governProjectOptions: [
        {
          dictValue: '政府投资',
          dictText: '政府投资'
        },
        {
          dictValue: '社会投资',
          dictText: '社会投资'
        }
      ],
      loading: false,
      showDialog: false
    }
  },
  computed: {
    civilTypeOptions() {
      return civilTypeOptions
    }
  },
  mounted() {
    this.getList()
  },
  activated() {
    if (this.$route.query.refresh) {
      this.getList()
      this.$router.replace({
        path: this.$route.path,
        query: { ...this.$route.query, refresh: undefined }
      })
    }
  },
  methods: {
    search() {
      this.parameter.title =
        this.parameter.title?.trim?.() || ''
      this.parameter.pageIndex = 1
      this.getList()
    },

    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },

    getList() {
      this.loading = true
      queryFileInfo(this.parameter).then((res) => {
        if (res.success) {
          this.total = res.data.count
          this.projectList = res.data.list
        }
        this.loading = false
      })
    },

    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },

    addProject() {
      this.$router.push({
        path: '/dataManagement/fileManagement/add',
        query: {
          title: '新增'
        }
      })
    },

    // 编辑
    editProject(row) {
      this.$router.push({
        path: '/dataManagement/fileManagement/add',
        query: {
          pkid: row.pkid,
          title: '编辑',
          data: row,
          isEdit: true
        }
      })
    },
    // 项目查看
    viewProject(row) {
      this.$router.push({
        path: '/dataManagement/fileManagement/add',
        query: {
          pkid: row.pkid,
          title: '查看',
          data: row,
          isEdit: true,
          isDetail: true
        }
      })
    },

    // 删除项目
    deleteProject(row) {
      this.$confirm(
        '此操作将永久删除该事件，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          deleteFileInfo(row.pkid).then((res) => {
            if (res.success) {
              this.$message.success('删除成功')
              if (
                this.total % 10 === 1 &&
                this.parameter.pageIndex ===
                  parseInt(this.total / 10) + 1 &&
                this.parameter.pageIndex !== 1
              ) {
                this.parameter.pageIndex -= 1
              }
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    },
    downloadFile() {
      if (!this.parameter.year) {
        this.$message.warning('导出请先选择年份')
        return
      }
      this.btnLoading = true
      downloadProject(this.parameter).then((res) => {
        const blob = new Blob([res])
        const a = document.createElement('a')
        a.href = URL.createObjectURL(blob)
        a.download = '固投目标作战图.xlsx'
        a.style.display = 'none'
        document.body.appendChild(a)
        a.click()
        a.remove()
        this.btnLoading = false
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
